package com.my.java.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;



public class RedisUtil {
	/*
        public static JedisPoolConfig c = new JedisPoolConfig(); // 连接池配置
        public static JedisPool jedisPool = null; // 连接池
        public static String REDIS_PROP_FILE_PATH = Constants.PROP_FILE_PATH+"/redis";
        
       static String BLOCK_WHEN_EXHAUSTED = PropertyUtil.getBundleProperties(REDIS_PROP_FILE_PATH,"BLOCK_WHEN_EXHAUSTED");
       static String LIFO = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"LIFO");
       public static String MAX_IDLE = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"MAX_IDLE");
       public static String MIN_IDLE = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"MIN_IDLE");
       public static String MAX_TOTAL = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"MAX_TOTAL");
       public static String MAX_WAIT_MILLIS = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"MAX_WAIT_MILLIS");
       public static String MIN_EVICTABLEIDLE_TIMEMILLIS = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"MIN_EVICTABLEIDLE_TIMEMILLIS");
       public static String TEST_ON_BORROW = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"TEST_ON_BORROW");
       public static String TEST_WHILE_IDLE = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"TEST_WHILE_IDLE");
       public static String SERVICE_IP = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"SERVICE_IP");
       public static String SERVICE_PORT = PropertiesKit.getBundleProperties(REDIS_PROP_FILE_PATH,"SERVICE_PORT");
       
        static {
            c.setBlockWhenExhausted(Boolean.parseBoolean(BLOCK_WHEN_EXHAUSTED)); // 连接耗尽则阻塞
            c.setLifo(Boolean.parseBoolean(LIFO)); // 后进先出
            c.setMaxIdle(Integer.parseInt(MAX_IDLE)); // 最大空闲连接数为10
            c.setMinIdle(Integer.parseInt(MIN_IDLE)); // 最小空闲连接数为0
            c.setMaxTotal(Integer.parseInt(MAX_TOTAL)); // 最大连接数为20
            c.setMaxWaitMillis(Integer.parseInt(MAX_WAIT_MILLIS)); // 设置最大等待毫秒数：无限制
            c.setMinEvictableIdleTimeMillis(Integer.parseInt(MIN_EVICTABLEIDLE_TIMEMILLIS)); // 逐出连接的最小空闲时间：30分钟
            c.setTestOnBorrow(Boolean.parseBoolean(TEST_ON_BORROW)); // 获取连接时是否检查连接的有效性：是
            c.setTestWhileIdle(Boolean.parseBoolean(TEST_WHILE_IDLE)); // 空闲时是否检查连接的有效性：是
            jedisPool = new JedisPool(c, SERVICE_IP, Integer.parseInt(SERVICE_PORT)); // 初始化连接池
        }
        
    
        public static Jedis getJedis() {
            return jedisPool.getResource();
        }
        
        public static void setObject(String key,Object obj){
                Jedis jedis = null;
                try {
                    jedis = RedisUtil.getJedis();
                    jedis.set(key, JsonKit.toJson(obj));
                } finally {
                    if (jedis != null) {
                        jedis.close();
                    }
                }
        }
        
        public static Object getObject(String key,Class clazz){
                Jedis jedis = null;
                Object obj = null;
                try {
                    jedis = RedisUtil.getJedis();
                    obj = JsonKit.toBean(jedis.get(key), clazz);
                } finally {
                    if (jedis != null) {
                        jedis.close();
                    }
                }
                return obj;
        }
        
        
        public static void main(String[] args) {
            // 操作Redis 
            Jedis jedis = null;
            try {
                jedis = RedisUtil.getJedis();
                jedis.set("myname", "Nick Huang");
                String rs = jedis.get("myname");
                System.out.println(rs);
            } finally {
                if (jedis != null) {
                    jedis.close();
                }
            }
        }*/
}
